<template>
  <div style="width: 100%">
    <div style="margin-bottom: 10px; margin-left: 10px">
      <el-button type="primary" :icon="Plus" circle @click="addForm" />
    </div>
    <el-table
      style="width: 100%"
      stripe
      border
      highlight-current-row
      :data="roleList"
    >
      <el-table-column
        align="center"
        label="id"
        prop="id"
        width="180"
      ></el-table-column>
      <el-table-column
        align="center"
        label="角色信息"
        prop="roleName"
        width="180"
      ></el-table-column>
      <el-table-column
        align="center"
        label="角色描述"
        prop="roleDesc"
        width="180"
      ></el-table-column>
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <!-- @click="handleEdit(scope.row)"  -->
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <!--scope.row当前行的对象-->
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        hide-on-single-page
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchModel.pageNow"
        :page-sizes="[5, 10, 30, 50]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </div>

    <!-- 编辑用户信息 -->
    <el-dialog
      v-model="editDialogVisible"
      :title="editTitle"
      width="30%"
      :before-close="handleClose"
    >
      <div class="tableItem" style="margin-top: 15px">
        <span>角色名称 : </span>
        <div class="innerInput" style="margin-left: 14px">
          <el-input v-model="editRole.roleName" placeholder="角色名称" />
        </div>
      </div>

      <div class="tableItem" style="margin-top: 15px">
        <span>角色描述 : </span>
        <div class="innerInput" style="margin-left: 14px">
          <el-input v-model="editRole.roleDesc" placeholder="描述" />
        </div>
      </div>

      

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="clearFrom">取消</el-button>
          <el-button type="primary" @click="chuangeRole"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import userApi from "../../api/user";
import roleApi from "@/api/role";
import { ElMessage } from "element-plus";

import { Plus } from "@element-plus/icons-vue";

export default {
  setup() {
    return {
      Plus,
    };
  },
  data() {
    return {
      roleList: [],
      editTitle: "",
      totalCount: 0, // 总数默认为0
      searchModel: {
        pageNow: 1,
        pageSize: 10,
        roleDesc: "",
      },
      editDialogVisible: false,
      editRole: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      roleApi.getRoleList(this.searchModel).then((res) => {
        this.roleList = res.data.records;
        this.totalCount = res.data.total;
      });
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNow) {
      this.searchModel.pageNow = pageNow;
      this.load();
    },
    handleEdit(editRole) {
      this.editTitle = "角色修改";
      (this.editDialogVisible = true), (this.editRole = editRole);
    },

    chuangeRole() {
      if (this.editTitle === "角色修改") {
        roleApi.updateRoleById(this.editRole).then((res) => {
          ElMessage({
            showClose: true,
            message: "修改成功！",
            type: "success",
          });
          this.load();
          this.clearFrom();
        });
      } else {
        roleApi.addRole(this.editRole).then((res) => {
          this.load();
          ElMessage({
            showClose: true,
            message: "添加成功",
            type: "success",
          });
        });
        this.clearFrom();
      }
    },
    addForm() {
      this.editTitle = "角色添加";
      this.editRole = [];
      this.editDialogVisible = true;
    },
    clearFrom() {
      (this.editDialogVisible = false),
        (this.editRole = ""),
        (this.editTitle = "");
    },
    handleDelete(id) {
      roleApi.removeRoleById(id).then((res) => {
        ElMessage({
          showClose: true,
          message: "删除成功！",
          type: "success",
        });
        this.load();
      });
    },
  },
};
</script>
<style>
.tableItem{
  display: flex;
  margin-top: 5px;
  text-align: center;
  margin-bottom: 8px;
  justify-content: center;
  
}
.tableItem span{
  margin-top: 5px;
  margin-right: 10px;
}
.innerInput{
  width: 300px;
}
</style>
